package net.wofly.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.GeneralSecurityException;

public class CryptUserUtil {
    
    public static final String CHARSET = "UTF-8";
    public static final String CRYPT_KEY = "gfds234#$#@erEERWE525dfe*&^";
    
    private static final Logger logger = LoggerFactory.getLogger(CryptUserUtil.class);
    
    /**
     * 采用的des加密生成用户密码
     *
     * @param inputString 密码字符串
     * @return
     */
    public static String generatePassword(String inputString) {
        try {
            return CryptHelper.desEncrypt(inputString, CHARSET, CRYPT_KEY);
        } catch (GeneralSecurityException e) {
            logger.error("生成密码时出错！");
        }
        return "";
    }
    
    /**
     * 验证输入的密码是否正确
     *
     * @param password    加密后的密码
     * @param inputString 输入的密码字符串
     * @return 验证结果，TRUE:正确    FALSE:错误
     */
    public static boolean validatePassword(String password, String inputString) {
        if (password.equals(generatePassword(inputString))) {
            return true;
        } else {
            return false;
        }
    }
    
//    public static void main(String[] args) throws GeneralSecurityException {
//
//        String p = CryptUserUtil.generatePassword("xulizhengxxulizhengxxulizhengx");
//        if (CryptUserUtil.validatePassword(p, "xulizheng")) {
//            System.out.println(true);
//        } else {
//            System.out.println(false);
//        }
//
//    }
    
}
